package com.heimali.sf.calendar;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.heimali.sf.R;
import com.heimali.sf.db.NoteHelper;
import com.heimali.sf.models.Note;
import com.heimali.sf.utils.AndroidUtils;

public class NoteDetailActivity extends Activity implements OnClickListener {

	public final static String INTENT_NOTE_ID = "note_id";
	
	private int noteID = 0;
	
	private TextView titleText;
	private TextView remarkText;
	private TextView timeText;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.note_detail);
		noteID = getIntent().getIntExtra(INTENT_NOTE_ID, 0);
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		Note item = getNoteByID(noteID);
		titleText.setText(item.name);
		remarkText.setText(item.remark);
		timeText.setText(item.date + " " + item.time);
	}

	public void initView() {

		/* title bar */
		ImageButton leftButton = (ImageButton) findViewById(R.id.left_btn);
		leftButton.setOnClickListener(this);
		((TextView) findViewById(R.id.title_text)).setText(R.string.note_detail);
		ImageView rightLine = (ImageView) findViewById(R.id.right_line);
		rightLine.setVisibility(View.VISIBLE);
		Button rightButton = (Button) findViewById(R.id.right_btn);
		rightButton.setVisibility(View.VISIBLE);
		rightButton.setText(R.string.note_delete);
		rightButton.setOnClickListener(this);
		
		
		titleText = (TextView) findViewById(R.id.note_title_text);
		remarkText = (TextView) findViewById(R.id.note_remark_text);
		timeText = (TextView) findViewById(R.id.note_time_text);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.left_btn:
			finish();
			break;
		case R.id.right_btn:
			showDelConfirmDialog();
			break;
		}
	}
	
	private void showDelConfirmDialog() {
		AndroidUtils.buildDialog(this, R.string.dialog_note_del_title, R.string.dialog_note_del_tip, R.string.dialog_ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				deleteNoteByID(noteID);
				NoteDetailActivity.this.finish();
			}
		}, R.string.dialog_cancel, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		}).show();
	}
	
	private void deleteNoteByID(int noteID) {
		NoteHelper.getInstance().delete(noteID);
	}
	
	private Note getNoteByID(int noteID) {
		return NoteHelper.getInstance().getItemByID(noteID);
	}
}
